<template>
  <div class="container">
    <div class="header">
      <el-input v-model="input" placeholder="请输入员工姓名" clearable>
        <i slot="prefix" class="el-input__icon el-icon-search"></i>
      </el-input>
      <div class="right">
        <el-link type="danger" class="elink" @click="handleDelete"
          >批量删除</el-link
        >
        <el-divider direction="vertical"></el-divider>
        <el-link type="primary" class="elink">批量启售</el-link>
        <el-divider direction="vertical"></el-divider>
        <el-link type="danger" class="elink">批量停售</el-link>
        <el-button
          icon="el-icon-plus"
          style="margin-left: 20px"
          @click="
            $router.push({ name: 'ChangeEmployee', query: { isEdit: '0' } })
          "
          >新建菜品</el-button
        >
      </div>
    </div>

    <el-table
      ref="multipleTable"
      :data="tableData"
      tooltip-effect="dark"
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55"> </el-table-column>
      <el-table-column prop="name" label="菜品名称"> </el-table-column>
      <el-table-column label="图片">
        <template slot-scope="scope">
          <el-image
            :src="env + '/common/download?name=' + scope.row.image"
            style="width: 40px; height: 40px"
          >
            <div slot="error" class="image-slot">
              <i class="el-icon-picture-outline"></i>
            </div>
          </el-image>
        </template>
      </el-table-column>
      <el-table-column prop="categoryName" label="菜品分类"> </el-table-column>
      <el-table-column prop="price" label="售价"> </el-table-column>
      <el-table-column label="售卖状态">
        <template slot-scope="scope">
          {{ scope.row.status ? "正常" : "已禁用" }}
        </template>
      </el-table-column>
      <el-table-column prop="updateTime" label="操作时间"> </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-link
            type="primary"
            @click="
              $router.push({ name: 'ChangeEmployee', query: { isEdit: '1' } })
            "
            >编辑</el-link
          >
          <el-divider direction="vertical"></el-divider>
          <el-popconfirm
            title="确认调整该账号的？"
            @onConfirm="handleChangeStatus(scope.row)"
          >
            <el-link slot="reference" type="danger">
              {{ scope.row.status ? "正常" : "已禁用" }}
            </el-link>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageInfo.page"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="pageInfo.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
  </div>
</template>

<script>
import { getDish, deleteDish } from "@/api/dish";
export default {
  data() {
    return {
      env: process.env.VUE_APP_BASE_API,
      input: "",
      tableData: [],
      currentPage4: 4,
      pageInfo: {
        page: 1,
        pageSize: 10,
      },
      multipleSelection: [],
      total: 0,
    };
  },
  created() {
    this.getData();
  },
  methods: {
    async getData() {
      const {
        data: { records, total },
      } = await getDish({ ...this.pageInfo, name: this.input });
      this.tableData = records;
      this.total = total;
    },
    handleSizeChange(val) {
      this.pageInfo.page = 1;
      this.pageInfo.pageSize = val;
      this.getData();
    },
    handleCurrentChange(val) {
      this.pageInfo.page = val;
      this.getData();
    },

    handleChangeStatus(row) {
      const data = { id: row.id, status: +!row.status };
      changeStatus(data).then(() => {
        this.$message.success("修改状态成功");
        this.getData();
      });
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    handleDelete() {
      deleteDish({
        ids: this.multipleSelection.map((item) => item.id).join(),
      }).then((res) => {
        this.$message.success("删除成功");
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.container {
  padding: 22px;
  .header {
    display: flex;
    justify-content: space-between;
    .el-input {
      width: 250px;
    }
  }
}
</style>
